﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SemiCASE.Business;

public partial class PopUp_CreateProject : SemiCASEBasePage
{
    private string ProjectID
    {
        get
        {
            return Request.QueryString["ProjectID"] != null ? Request.QueryString["ProjectID"] : String.Empty;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadStatus();

            if (!String.IsNullOrEmpty(this.ProjectID))
                LoadProjectInfo();

        }
    }

    private void LoadStatus()
    {
        DataTable dt = new ProjectBS().LoadStatus();
        if (dt.Rows.Count > 0)
        {
            DropDownListStatus.DataSource = dt;
            DropDownListStatus.DataBind();
            DropDownListStatus.Items.Insert(0, new ListItem("Please select status", "0"));
        }
        else
        {
            DropDownListStatus.DataSource = null;
            DropDownListStatus.DataBind();
        }
    }

    private void LoadProjectInfo()
    {
        if (String.IsNullOrEmpty(this.ProjectID))
            return;

        DataTable dt = new ProjectBS().SelectProjectInfo(new Guid(this.ProjectID));

        if (dt.Rows.Count == 0)
            return;

        DataRow row = dt.Rows[0];
        TextBoxProjectName.Text = (row["Name"] != DBNull.Value) ? row["Name"].ToString() : String.Empty;
        TarihStartDate.ucoText = (row["StartDate"] != DBNull.Value) ? row["StartDate"].ToString() : String.Empty;
        TarihEndDate.ucoText = (row["EndDate"] != DBNull.Value) ? row["EndDate"].ToString() : String.Empty;
        if (row["StatusID"] != DBNull.Value)
        {
            (DropDownListStatus.Items.FindByValue(row["StatusID"].ToString())).Selected = true;
        }
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(this.ProjectID))
            UpdateProject();
        else
            SaveProject();
    }

    private void SaveProject()
    {
        if (!ValidateEnteredProjectInfo())
            return;

        new ProjectBS().CreateProject(Guid.NewGuid(),
                                          TextBoxProjectName.Text,
                                          TarihStartDate.ucoTarih,
                                          TarihEndDate.ucoTarih,
                                          Convert.ToInt32(DropDownListStatus.SelectedItem.Value));
        
        JavascriptHelper.PopUpiKapatAcanPencereyiRefresh();
    }

    private void UpdateProject()
    {
        if (!ValidateEnteredProjectInfo())
            return;

        new ProjectBS().UpdateProject(new Guid(this.ProjectID),
                                          TextBoxProjectName.Text,
                                          TarihStartDate.ucoTarih,
                                          TarihEndDate.ucoTarih,
                                          Convert.ToInt32(DropDownListStatus.SelectedItem.Value));
        
        JavascriptHelper.PopUpiKapatAcanPencereyiRefresh();
    }

    private bool ValidateEnteredProjectInfo()
    {
        if (String.IsNullOrEmpty(TextBoxProjectName.Text))
        {
            MessageBox("Please enter project name", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
            return false;
        }

        if (String.IsNullOrEmpty(TarihStartDate.ucoText))
        {
            MessageBox("Please enter start date", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
            return false;
        }

        if (String.IsNullOrEmpty(TarihEndDate.ucoText))
        {
            MessageBox("Please enter end date", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
            return false;
        }

        if (DropDownListStatus.SelectedItem.Value == "0")
        {
            MessageBox("Please enter status", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
            return false;
        }

        return true;
    }
}